<?php

/**
 * alltosun.com store_helper.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: $
 * $Id$
 */
class stat_helper
{
    /**
     * 获取分店的所有进货信息
     * @param $store_id
     */
    public static function get_manager_all_purchase($store_id) {
        $store_id += 0;

        //每月进货商品信息
        $where = "store_id = $store_id AND status = 10";

        $arr = _model('order')->getRow("select sum(price) as purchase_money, count(*) as purchase_order_count, sum(goods_num) as purchase_num from `order` WHERE $where");

        return $arr;
    }

    /**
     * 分店的每月进货信息
     */
    public static function get_manager_month_purchase($store_id, $month_start_ts = 0, $month_end_ts = 0) {
        // 存数据库
        // 存ssdb/redis
        // 存memcached, 查询结果,存入数据库
        // 更新 查询

        $month_start_ts += 0;
        $month_end_ts += 0;

        if ($month_start_ts > 0) {
            $s = $month_start_ts;
        } else {
            $s = mktime(0, 0, 0, date("m"), 1, date("Y"));
        }

        if ($month_end_ts > 0) {
            $e = $month_end_ts;
        } else {
            $e = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
        }

        $month_start = datetime($s);
        $month_end = datetime($e);

        $store_id += 0;

        //每月进货商品信息
        $where = "store_id = $store_id AND status = 10 AND deliver_time >= '$month_start' AND deliver_time <= '$month_end'";

        $arr = _model('order')->getRow("select sum(price) as purchase_money, count(*) as purchase_order_count, sum(goods_num) as purchase_num from `order` WHERE $where");

        return $arr;

        $ret = $arr;

        //每月进货箱数
        $where = "`order`.store_id = $store_id AND `order`.status = 10 AND `order`.deliver_time >= '$month_start' AND `order`.deliver_time <= '$month_end' AND order_relation.order_id = `order`.id";

        $arr = _model('order_relation')->getRow("select sum(order_relation.goods_num) as purchase_num from `order_relation`, `order` WHERE $where");

        $ret['purchase_num'] = $arr['purchase_num'];

        return $ret;

    }


    /**
     * 分店的在售商品种类信息
     */
    public static function get_manager_goods_category_num($store_id) {
        if (!$store_id) {
            return 0;
        }
        $store_info = _uri('store', $store_id);
        if (!$store_info) {
            return 0;
        }
        $filter = array(
            'user_id' => $store_info['user_id'],
            'progress' => 10
        );
        $order_ids = _model('order')->getFields('id', $filter);
        if (!$order_ids) {
            return 0;
        }

        $order_relation_filter = array(
            'order_id' => $order_ids
        );
        $sku_ids = _model('order_relation')->getFields('sku_id', $order_relation_filter);
        $sku_unqiue_ids = array_unique($sku_ids);
        return count($sku_unqiue_ids);
    }


    /**
     * 分店的每月核销数量
     */
    public static function get_manager_month_vocher_num($store_id, $month_start_ts = 0, $month_end_ts = 0) {
        return 0;

        $month_start_ts += 0;
        $month_end_ts += 0;

        if ($month_start_ts > 0) {
            $s = $month_start_ts;
        } else {
            $s = mktime(0, 0, 0, date("m"), 1, date("Y"));
        }

        if ($month_end_ts > 0) {
            $e = $month_end_ts;
        } else {
            $e = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
        }

        $month_start = datetime($s);
        $month_end = datetime($e);

        $store_id += 0;

        //每月进货商品信息
        $where = "store_id = $store_id AND status = 10 AND deliver_time >= '$month_start' AND deliver_time <= '$month_end'";

        $arr = _model('order')->getRow("select sum(price) as purchase_money, count(*) as purchase_order_count from `order` WHERE $where");

    }

    /**
     * 总店下的所有分店 的采购信息
     * @param $owner_id
     */
    public static function get_owner_all_purchase($owner_id) {
        $stores = store_helper::get_owner_stores($owner_id);

        if ($stores) {
            $id = '';
            foreach ($stores as $store) {
                $id .= $store['id'] . ',';
            }
            $stores_id = rtrim($id, ',');
        } else {
            return array(
                'purchase_money' => '0.00',
                'purchase_order_count' => '0',
                'purchase_num' => '0',
            );

            $stores_id = '0';
        }
        $where = "O.store_id IN ($stores_id) AND O.status = 10";

        $sql = "select IFNULL(sum(O.price), 0)  as purchase_money, count(O.id) as purchase_order_count, IFNULL(sum(O.goods_num), 0) as purchase_num from `order` as O WHERE $where";
        $arr = _model('order')->getRow($sql);

        return $arr;
    }


    /**
     * 经销商/销售人员的客户数
     */
    public static function get_customer_num($role_type, $user_id) {
        $filter = array(
            'status' => array(
                store_config::STATUS_NORMAL,
                store_config::STATUS_TRIAL,
            ),
        );

        if ($role_type == user_config::AGENCY) {
            $filter['agency_id'] = $user_id;
        } else {
            if ($role_type == user_config::MARKETING) {
                $filter['marketing_id'] = $user_id;
            }
        }

        $num = _model('user_store')->getTotal($filter);
        return $num + 0;

    }


    /**
     * 经销商/销售人员的终端数
     */
    public static function get_store_num($role_type, $user_id) {
        $filter = array(
            'status' => array(1, 2, 3, 4),
            'is_delete' => 1
        );

        if ($role_type == user_config::AGENCY) {
            $filter['agency_id'] = $user_id;
        } else {
            if ($role_type == user_config::MARKETING) {
                $filter['marketing_id'] = $user_id;
            }
        }

        $num = _model('store')->getTotal($filter);
        return $num + 0;

    }

    /**
     * 获取总店的总积分
     */
    public static function get_user_store_score($owner_id) {
        if (!$owner_id) {
            return false;
        }

        // 获取总店下的所有分店
        $store_list = store_helper::get_owner_stores($owner_id);

        $score = 0;
        if ($store_list) {
            foreach ($store_list as $k => $v) {
                $user_id = $v['user_id'];
                // 获取用户信息
                $user_info = user_helper::get_user_info($user_id);
                $score += $user_info['score'];
            }
        }

        return $score;

    }


    /**
     * 经销商的市场人员数
     */
    public static function get_marketing_num($user_id) {
        $filter = array(
            'status' => 1,
            'agency_id' => $user_id,
            'is_delete' => 1
        );


        $num = _model('user_marketing')->getTotal($filter);
        return $num + 0;
    }

    /**
     * 分店的库存数量
     */
    public static function get_stock_num($user_store_id) {
        // $store_id += 0;
        // $where = "store_id = '{$store_id}'";
        // $arr = _model('store_stock')->getRow("select sum(stock) as stock from store_stock WHERE $where");
        // return @$arr['stock'] + 0;
        $filter = array(
            'store_id' => $user_store_id
        );
        $store_goods_sku = _model('store_goods_sku')->getList($filter);
        $count_stock_num = 0;
        foreach ($store_goods_sku as $key => $value) {
            $count_stock_num += $value['stock'];
        }
        return $count_stock_num;
    }


    /**
     * 分店的库存商品种类
     */
    public static function get_goods_category_num($store_id) {
        $store_id += 0;
        $where = "store_id = '{$store_id}'";
        $arr = _model('store_stock')->getRow("select count(*) as stock from store_stock WHERE $where");
        return @$arr['stock'] + 0;
    }


    /**
     * 经销商/市场人员的每月销售数量 (商店的进货数量)
     */
    public static function get_agency_month_sale($role_type, $user_id, $month_start_ts = 0, $month_end_ts = 0) {

        $month_start_ts += 0;
        $month_end_ts += 0;

        if ($month_start_ts > 0) {
            $s = $month_start_ts;
        } else {
            $s = mktime(0, 0, 0, date("m"), 1, date("Y"));
        }

        if ($month_end_ts > 0) {
            $e = $month_end_ts;
        } else {
            $e = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
        }

        $month_start = datetime($s);
        $month_end = datetime($e);

        if ($role_type == user_config::AGENCY) {
            $user_where = "S.agency_id = $user_id";
        } else {
            if ($role_type == user_config::MARKETING) {
                $user_where = "S.marketing_id = $user_id";
            } else {
                if ($role_type == user_config::STORE_OWNER) {
                    $user_where = "S.owner_id = $user_id";
                } else {
                    if ($role_type == user_config::STORE_MANAGER) {
                        $user_where = "S.user_id = $user_id";
                    } else {
                        return false;
                    }
                }
            }
        }

        //每月进货商品信息
        $where = "O.store_id = S.id AND O.status = 10 AND O.deliver_time >= '$month_start' AND O.deliver_time <= '$month_end' AND $user_where";

        $arr = _model('order')->getRow("select IFNULL(sum(O.price),0) as purchase_money, count(O.id) as purchase_order_count, IFNULL(sum(O.goods_num),0) as purchase_num from `order` as O, `store` as S WHERE $where");

        return $arr;
    }


    /**
     * 经销商/市场人员的每天销售数量 (商店的进货数量)
     */
    public static function get_agency_daily_sale($role_type, $user_id, $day_start_ts = 0, $day_end_ts = 0) {

        $day_start_ts += 0;
        $day_end_ts += 0;

        if ($day_start_ts > 0) {
            $s = $day_start_ts;
        } else {
            $s = mktime(0, 0, 0);
        }

        if ($day_end_ts > 0) {
            $e = $day_end_ts;
        } else {
            $e = mktime(23, 59, 59);
        }

        $month_start = datetime($s);
        $month_end = datetime($e);

        if ($role_type == user_config::AGENCY) {
            $user_where = "S.agency_id = $user_id";
        } else {
            if ($role_type == user_config::MARKETING) {
                $user_where = "S.marketing_id = $user_id";
            } else {
                if ($role_type == user_config::STORE_OWNER) {
                    $user_where = "S.owner_id = $user_id";
                } else {
                    if ($role_type == user_config::STORE_MANAGER) {
                        $user_where = "S.user_id = $user_id";
                    } else {
                        return false;
                    }
                }
            }
        }

        //每月进货商品信息
        $where = "O.store_id = S.id AND O.status = 10 AND O.deliver_time >= '$month_start' AND O.deliver_time <= '$month_end' AND $user_where";

        $arr = _model('order')->getRow("select IFNULL(sum(O.price),0) as purchase_money, count(O.id) as purchase_order_count, IFNULL(sum(O.goods_num),0) as purchase_num from `order` as O, `store` as S WHERE $where");

        return $arr;
    }

    public static function get_agency_yesterday_sale($role_type, $user_id) {
        $t = time() - 86400;
        $Y = date("Y", $t);
        $N = date("n", $t);
        $J = date("j", $t);
        $ts_start = mktime(0, 0, 0, $N, $J, $Y);
        $ts_end = mktime(23, 59, 59, $N, $J, $Y);
        return $sale_arr_yesterday = self::get_agency_daily_sale($role_type, $user_id, $ts_start, $ts_end);
    }


    /**
     * 经销商的 未处理的订单
     */
    public static function get_agency_unassigned_num($user_id) {
        $filter = array(
            'agency_id' => $user_id,
            'is_assigned' => 0,
            'progress' => 1,
            'status' => 1
        );

        $order_num = _model('order')->getTotal($filter);
        return $order_num;
    }

    /**
     * 经销商的 未发货订单数
     */
    public static function get_agency_no_deliver_num($user_id) {
        $filter = array(
            'agency_id' => $user_id,
            'status' => 1
        );

        $order_num = _model('order')->getTotal($filter);
        return $order_num;
    }

    /**
     * 经销商进货记录数
     */
    public static function get_agency_trade_order_num($user_id) {
        $filter = array(
            'user_id' => $user_id,
            'progress !=' => 15,
            'status' => 1
        );
        $order_num = _model('order')->getTotal($filter);
        return $order_num;
    }

    /**
     * 市场人员未配送的订单数目
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public static function get_marketing_no_deliver_num($user_id) {
        $filter = array(
            'assigned_id' => $user_id,
            'status' => 1,
            'progress' => array(1, 5)
        );
        $order_num = _model('order')->getTotal($filter);
        return $order_num;
    }

    /**
     * 市场人员负责的店铺数目
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public static function get_marketing_store_num($user_id) {
        $filter = array(
            'marketing_id' => $user_id
        );

        $store_num = _model('store')->getTotal($filter);
        return $store_num;
    }

    /**
     * 获取未读消息数
     */
    public static function get_unread_msg_num($user_id) {
        $filter = array(
            'user_id' => $user_id,
            'is_read' => 1,
        );

        $num = _model('message_user')->getTotal($filter);
        return $num + 0;

        $arr = _model('message_user')->getRow("select count(*) as cnt, is_read from `message_user` WHERE user_id='{$user_id}' group by is_read");

        $read_num = $noread_num = $all_num = 0;
        if ($arr) {
            foreach ($arr as $k => $v) {
                if ($v['is_read'] == 1) {
                    $read_num = $v['cnt'];
                } elseif ($v['is_read'] == 0) {
                    $noread_num = $v['cnt'];
                }
            }

            $all_num = $read_num + $noread_num;
            return array(
                // 'noread_num' => $noread_num,
                'noread_num' => message_center_helper::get_unread_message_num($user_id),
                'read_num' => $read_num,
                'all_num' => $all_num,
            );
        }
    }


    /**
     *  xdq -2 合伙人旗下所有影院门店订单欠款总额 未付款
     */
    public static function count_agency_unpayed($agency_id) {

        $filter = array(
            'agency_id' => $agency_id,
            'is_pay' => 0
        );

        //合伙人旗下所有影院门店订单欠款总额 未付款
        $res = _model('order')->getFields('price', $filter);

        //$unpayed_sum = _model('order')->getRow("select IFNULL(sum(O.price),0) as unpayed_money from `order` WHERE user_id='{$user_id}'and is_pay=0 ");

        return array_sum($res);
    }

}